package com.share.lottery.activity.caiborecommond;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.share.lottery.R;
import com.share.lottery.adapter.CpSelectParentAdapter;
import com.share.lottery.adapter.CpSelectSubAdapter;
import com.share.lottery.adapter.MyFragmentPagerAdapter;
import com.share.lottery.base.Constants;
import com.share.lottery.model.LotteryBean;
import com.share.lottery.model.LotteryTypeBean;
import com.share.lottery.model.User;
import com.share.lottery.view.CustomToast;

public class CaibaorereCommendActivity extends FragmentActivity {
	private TextView tv0;
	private TextView tv1;
	private TextView tv2;
	private int type = 0;
	private ImageButton imgLeft;
	private TextView tvAdd;
	private TextView tvCenter;
	public ViewPager mPager;
	private ImageView image;
	private int bmpW;
	private ArrayList<Fragment> fragmentList;
	private RelativeLayout layout0;
	private RelativeLayout layout1;
	private RelativeLayout layout2;
	private ImageView tab_slider;
	private int offset;
	private CommondedFragment commondedFragment;
	private CommondingFragment commondingFragment;
	private StatisticsFragment statisticsFragment;
	private String[] parent;
	private int currIndex = 0;
	private List<List<LotteryBean>> subLists = new ArrayList<List<LotteryBean>>();
	private ImageView imgSx;//筛选
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.commend_main);
		subLists.clear();
		init();

	}

	private void init() {
		getData();
		initTitle();
		InitItem_bg();
		InitViewPager();
		setonClicker();

	}

	/**
	 *点击事件处理 
	 */
	private void setonClicker() {

		imgSx.setOnClickListener(new OnClickListener() {


			public void onClick(View v) {

				showSx();
			}
		});

	}

	private void getData() {
	//	CaiboRmdController.getInstance().caibaoRmdLottery(handler);

	}
	private void doMessage(Message msg) {
		switch (msg.arg1) {
		case Constants.HTTP_OK://请求成功
			List<LotteryTypeBean> beanList = (List<LotteryTypeBean>) msg.obj;
			if (beanList != null) {
				parent = new String[beanList.size()];
				int tag  = 0;
				for (LotteryTypeBean lotteryTypeBean : beanList) {
					parent[tag] = lotteryTypeBean.getTypename();
					subLists.add(lotteryTypeBean.getLotteries());
					tag ++;
					Log.i("test", "猜中名=="+lotteryTypeBean.getTypename());
				}
			}


			break;
		case Constants.HTTP_ERROR://请求错误
			String serror = (String) msg.obj;

			break;
		case Constants.HTTP_ERROR_PASER://数据解析错误

			break;
		case Constants.HTTP_ERROR_NET://网络错误


			break;

		default:
			break;
		}

	}

	Handler handler = new Handler(){
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case Constants.CAIBO_RMD_LOTTERY:
				doMessage(msg);
				break;

			default:
				break;
			}

		}


	};
	private TextView titleTv;
	public void initTitle() {
		imgSx = (ImageView) findViewById(R.id.img_filter);
		ImageButton titleLeftImg = (ImageButton) findViewById(R.id.title_left_img);
		ImageButton titleRightImg = (ImageButton) findViewById(R.id.title_right_img);
		titleTv = (TextView) findViewById(R.id.title_tv_center);

		titleLeftImg.setImageResource(R.drawable.back);
		titleTv.setText("彩宝推荐");
		titleRightImg.setVisibility(View.VISIBLE);
		titleRightImg.setImageResource(R.drawable.buy);
		titleRightImg.setOnClickListener(new OnClickListener() {
			
		
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), BuyCaibotjActivity.class);
				startActivity(intent);
			}
		});
		titleLeftImg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				finish();

			}
		});
	}



	public int getScreenWidth1() {
		WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

		int width = wm.getDefaultDisplay().getWidth();// 屏幕宽度
		return width;
	}

	/**
	 * 初始化tab滑动背景
	 */
	private void InitItem_bg() {

		tab_slider = (ImageView) findViewById(R.id.tab_main_slider);
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.myvideos_topselection);
		offset = (getScreenWidth1() / 3);

		float scale_w = (float) offset / bitmap.getWidth();
		Matrix matrix = new Matrix();
		matrix.postScale(scale_w, 0.85f);
		tab_slider.setImageMatrix(matrix);// 控制iteam_bg在不同的屏自适应

		layout0 = (RelativeLayout) findViewById(R.id.destn_select0);
		layout1 = (RelativeLayout) findViewById(R.id.destn_select1);
		layout2 = (RelativeLayout) findViewById(R.id.destn_select2);

		// layout0.setOnClickListener(changeListener);
		// layout1.setOnClickListener(changeListener);
		// layout2.setOnClickListener(changeListener);

		layout0.setOnClickListener(new txListener(0));
		layout1.setOnClickListener(new txListener(1));
		layout2.setOnClickListener(new txListener(2));

		tv0 = (TextView) findViewById(R.id.tv0);
		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);



	}

	/********************************************************************************************/
	public class txListener implements View.OnClickListener {
		private int index = 0;

		public txListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mPager.setCurrentItem(index);
		}
	}

	/*
	 * 初始化ViewPager
	 */
	private User user = null;

	public void InitViewPager() {


		image = (ImageView) findViewById(R.id.tab_main_slider);
		mPager = (ViewPager) findViewById(R.id.viewpager);
		fragmentList = new ArrayList<Fragment>();
		commondedFragment = new CommondedFragment();
		commondingFragment = new CommondingFragment();
		statisticsFragment = new StatisticsFragment();

		fragmentList.add(commondingFragment);
		fragmentList.add(commondedFragment);
		fragmentList.add(statisticsFragment);


		// 给ViewPager设置适配器
		mPager.setAdapter(new MyFragmentPagerAdapter(
				getSupportFragmentManager(), fragmentList));
		mPager.setCurrentItem(0);// 设置当前显示标签页为第一页
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());// 页面变化时的监听器

		mPager.setOffscreenPageLimit(2);

		switch (currIndex) {
		case 0:
			layout0.performClick();
			break;
		case 1:
			layout1.performClick();
			break;
		case 2:
			layout2.performClick();
			break;

		default:
			break;
		}
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {
		private int one = offset + bmpW;// 两个相邻页面的偏移量

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
			Animation animation = new TranslateAnimation(currIndex * one, arg0
					* one, 0, 0);// 平移动画
			currIndex = arg0;
			animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
			animation.setDuration(200);// 动画持续时间0.2秒
			image.startAnimation(animation);// 是用ImageView来显示动画的

			switch (arg0) {
			case 0:
				imgSx.setVisibility(View.VISIBLE);
				tv0.setTextColor(getResources().getColor(R.color.TextColorBlue));
				tv1.setTextColor(getResources()
						.getColor(R.color.TextColorBlack));
				tv2.setTextColor(getResources()
						.getColor(R.color.TextColorBlack));
				break;
			case 1:
				imgSx.setVisibility(View.VISIBLE);
				tv1.setTextColor(getResources().getColor(R.color.TextColorBlue));
				tv0.setTextColor(getResources()
						.getColor(R.color.TextColorBlack));
				tv2.setTextColor(getResources()
						.getColor(R.color.TextColorBlack));
				break;
			case 2:
				imgSx.setVisibility(View.GONE);
				tv2.setTextColor(getResources().getColor(R.color.TextColorBlue));
				tv0.setTextColor(getResources()
						.getColor(R.color.TextColorBlack));
				tv1.setTextColor(getResources()
						.getColor(R.color.TextColorBlack));
				break;

			default:
				break;
			}
		}
	}

	private void showSx() {
		if (parent == null) {
			CustomToast.makeText(getApplicationContext(), "未获取到筛选数据", 0);
			return;
		}
		ListView parentListView = null;
		View view = this.getLayoutInflater()
				.inflate(R.layout.area_select, null);

		final PopupWindow popupWindow = new PopupWindow(view,LayoutParams.FILL_PARENT, 300, true);
		popupWindow.setBackgroundDrawable(new ColorDrawable(000000));
		// 设置点击弹框外部，弹框消失
		popupWindow.setOutsideTouchable(true);
		popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		// 设置焦点
		popupWindow.setFocusable(true);


		parentListView = (ListView) view.findViewById(R.id.parent_listView);
		final ListView subtListView = (ListView) view
				.findViewById(R.id.sub_ListView);

		CpSelectParentAdapter parentAdapter = new CpSelectParentAdapter(this, parent);
		parentListView.setAdapter(parentAdapter);
		final CpSelectSubAdapter selectSubAdapter = new CpSelectSubAdapter(this);
		subtListView.setAdapter(selectSubAdapter);
		parentListView.setOnItemClickListener(new OnItemClickListener() {


			public void onItemClick(AdapterView<?> arg0, View arg1, int positon,
					long arg3) {
				selectSubAdapter.clearData();
				List<LotteryBean> datas = subLists.get(positon);
				selectSubAdapter.addData(datas);
				subtListView.setOnItemClickListener(new OnItemClickListener() {


					public void onItemClick(AdapterView<?> arg0, View arg1,
							int subposition, long arg3) {
						LotteryBean lotteryBean = (LotteryBean) arg0.getAdapter().getItem(subposition);
						String lottetyname = lotteryBean.getName();
						Message message = new Message();
						message.obj = lottetyname;
						if (currIndex == 0) {
							message.what = 0;
							CommondingFragment.updeUihandler.sendMessage(message);
						}else if (currIndex == 1) {
							message.what = 1;
							CommondedFragment.updeUihandler.sendMessage(message);

						}
						
						popupWindow.dismiss();

					}
				});


			}
		});
		popupWindow.showAsDropDown(tab_slider);


	}


}
